﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using appDataContracts;

namespace appSupplier
{
    /// <summary>
    /// Logique d'interaction pour AddProductWindow.xaml
    /// </summary>
    public partial class AddProductWindow : Window
    {
        private int catalogId;
        private ShowProductsWindow parent;

        public AddProductWindow(int catalogId, string catalogName, ShowProductsWindow parent)
        {
            InitializeComponent();

            this.parent = parent;
            this.catalogId = catalogId;

            List<SerializedProduct> productList = AppWindow.supplierService.getSupplierProducts(AppWindow.supplier.SUPPLIERID);
            List<SerializedProduct> productsInCatalog = AppWindow.supplierService.getCatalogProducts(catalogId);
            List<SerializedProduct> temp = new List<SerializedProduct>();

            // supprime les produits déjà dans le catalogue de la liste
            foreach (SerializedProduct productInCatalog in productsInCatalog)
            {
                foreach (SerializedProduct product in productList)
                {
                    if (product.PRODUCTID == productInCatalog.PRODUCTID)
                    {
                        temp.Add(product);
                    }
                }
            }

            foreach (SerializedProduct product in temp)
            {
                productList.Remove(product);
            }

            dgProducts.ItemsSource = productList;
        }

        private void btBack_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btAdd_Click(object sender, RoutedEventArgs e)
        {
            int productId = ((SerializedProduct)dgProducts.SelectedItem).PRODUCTID;
            AppWindow.supplierService.addProductInCatalog(productId, catalogId);

            #region update grid

            List<SerializedProduct> productList = AppWindow.supplierService.getSupplierProducts(AppWindow.supplier.SUPPLIERID);
            List<SerializedProduct> productsInCatalog = AppWindow.supplierService.getCatalogProducts(catalogId);
            List<SerializedProduct> temp = new List<SerializedProduct>();

            // supprime les produits déjà dans le catalogue de la liste
            foreach (SerializedProduct productInCatalog in productsInCatalog)
            {
                foreach (SerializedProduct product in productList)
                {
                    if (product.PRODUCTID == productInCatalog.PRODUCTID)
                    {
                        temp.Add(product);
                    }
                }
            }

            foreach (SerializedProduct product in temp)
            {
                productList.Remove(product);
            }

            dgProducts.ItemsSource = productList; 
            #endregion

        }

        private void dgProducts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgProducts.SelectedItems.Count > 0)
            {
                btAdd.Visibility = Visibility.Visible;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            parent.update();
        }
    }
}
